# Maintainer: Andreas 'Segaja' Schleifer <segaja at archlinux dot org>

_gemname='stringio'
pkgname="ruby-${_gemname}"
pkgver=3.0.2
pkgrel=4
pkgdesc='Pseudo `IO` class from/to `String`.'
arch=('loong64' 'x86_64')
url="https://github.com/ruby/${_gemname}"
license=('BSD' 'RUBY')
depends=('ruby')
makedepends=('ruby-bundler' 'ruby-rake' 'ruby-rake-compiler')
checkdepends=('ruby-test-unit')
options=(!emptydirs)
source=("${url}/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz")
sha512sums=('a9ac2820ce1c2bd55aac032be42a493a910787ed04bc41f35ec33b19ffd878c925dbbca49d6c27b07cea5ffb8fa98ecdb576738c9654587dea6842e3bd1f486c')

prepare() {
  cd "${_gemname}-${pkgver}"

  # update gemspec/Gemfile to allow newer version of the dependencies
  sed --in-place --regexp-extended 's|~>|>=|g' "${_gemname}.gemspec"

  rm --verbose \
    rakelib/changelogs.rake \
    rakelib/epoch.rake
}

build() {
  cd "${_gemname}-${pkgver}"

  rake compile
  rake build
}

check() {
  cd "${_gemname}-${pkgver}"

  rake test
}

package() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"
  local _platform="$(gem env platform | cut -d':' -f2)"
  local _extension_api_version="$(ruby -e 'puts Gem.extension_api_version')"

  gem install \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "${pkgdir}/${_gemdir}" \
    --bindir "${pkgdir}/usr/bin" \
    "pkg/${_gemname}-${pkgver}.gem"

  install --verbose --directory --mode=0755 \
    "${pkgdir}/usr/lib/ruby/${_extension_api_version}/${_platform}" \
    "${pkgdir}${_gemdir}/specifications/default"

  mv --verbose "${pkgdir}${_gemdir}/specifications/${_gemname}-${pkgver}.gemspec" "${pkgdir}${_gemdir}/specifications/default/${_gemname}-${pkgver}.gemspec"
  mv --verbose "${pkgdir}${_gemdir}/extensions/${_platform}/${_extension_api_version}/${_gemname}-${pkgver}/${_gemname}.so" "${pkgdir}/usr/lib/ruby/${_extension_api_version}/${_platform}/${_gemname}.so"

  # remove unrepreducible files
  rm --force --recursive --verbose \
    "${pkgdir}${_gemdir}/cache/" \
    "${pkgdir}${_gemdir}/build_info/" \
    "${pkgdir}${_gemdir}/extensions/" \
    "${pkgdir}${_gemdir}/gems/" \
    "${pkgdir}${_gemdir}/plugins/" \
    "${pkgdir}${_gemdir}/doc/${_gemname}-${pkgver}/ri/ext/"

  install --verbose -D --mode=0644 LICENSE.txt "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  install --verbose -D --mode=0644 COPYING *.md --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}
